# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=paraview
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.11.1
pkgrel=5
pkgdesc="Parallel Data Analysis and Visualization Application based on VTK. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.paraview.org/'
license=('custom')
optdepends=("${MINGW_PACKAGE_PREFIX}-python-numpy"
            "${MINGW_PACKAGE_PREFIX}-python-matplotlib")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-cgns"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-fmt"
         "${MINGW_PACKAGE_PREFIX}-gdal"
         "${MINGW_PACKAGE_PREFIX}-gl2ps"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libogg"
         "${MINGW_PACKAGE_PREFIX}-liblas"
         "${MINGW_PACKAGE_PREFIX}-libtheora"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-openturns"
         "${MINGW_PACKAGE_PREFIX}-pdal"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-qt5-svg"
         "${MINGW_PACKAGE_PREFIX}-qt5-tools"
         "${MINGW_PACKAGE_PREFIX}-tbb"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt5-xmlpatterns"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-cli11"
             "${MINGW_PACKAGE_PREFIX}-libharu"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json"
             "${MINGW_PACKAGE_PREFIX}-utf8cpp")
source=("https://www.paraview.org/files/v${pkgver%.*}/ParaView-v${pkgver}.tar.xz"
        "001-dll-export-some-vtk-functions.patch"
        "002-Fix-build-with-gcc-13.patch"
        "https://gitlab.kitware.com/vtk/vtk/-/merge_requests/10304.patch")
sha256sums=('5cc2209f7fa37cd3155d199ff6c3590620c12ca4da732ef7698dec37fa8dbb34'
            'a9b9292be90c259f0aa058e3f4a7a08b6218f20e6cb2a71e214acda2d2302a50'
            '20733e97ccf9b0a8e615c75936ce5e4c1db0ac00e9cac30bc359d5912da38990'
            '2f2ee0a2450d190dd6eaab7efef615fe85c8099e28c3dae5d35db6303cc82bfe')

prepare() {
  cd "${srcdir}"/ParaView-v${pkgver}

  cd VTK
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    # Copied from mingw-w64-vtk/007-dll-export-some-functions.patch
    patch -Np1 -i "${srcdir}"/001-dll-export-some-vtk-functions.patch
  fi
  patch -p1 -i "${srcdir}"/002-Fix-build-with-gcc-13.patch
  # Fix building against OpenTURNS 1.21
  patch -p1 -i "${srcdir}"/10304.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")

  CXXFLAGS+=" -w -Wno-register" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DCMAKE_CXX_STANDARD=17 \
      -DVTK_IGNORE_CMAKE_CXX11_CHECKS=ON \
      -DPARAVIEW_BUILD_SHARED_LIBS=ON \
      -DPARAVIEW_USE_PYTHON=ON \
      -DPARAVIEW_USE_QT=ON \
      -DPARAVIEW_USE_MPI=OFF \
      -DPARAVIEW_ENABLE_GDAL=ON \
      -DPARAVIEW_ENABLE_LAS=ON \
      -DPARAVIEW_ENABLE_OPENTURNS=ON \
      -DPARAVIEW_ENABLE_PDAL=ON \
      -DPARAVIEW_VERSIONED_INSTALL=OFF \
      -DPARAVIEW_CUSTOM_LIBRARY_SUFFIX="pv${pkgver%.*}" \
      -DPARAVIEW_BUILD_WITH_EXTERNAL=ON \
      -DPARAVIEW_PYTHON_SITE_PACKAGES_SUFFIX="lib/python${_pyver}/site-packages/pv/" \
      -DVTK_SMP_IMPLEMENTATION_TYPE=TBB \
      -DVTK_MODULE_USE_EXTERNAL_VTK_exprtk=OFF \
      -DVTK_MODULE_USE_EXTERNAL_VTK_ioss=OFF \
      -DVTK_MODULE_USE_EXTERNAL_VTK_verdict=OFF \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      ../ParaView-v${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  MSYS2_ARG_CONV_EXCL="-p" python -m compileall \
    -o 0 -o 1 -q -s"${pkgdir}" -p"/" "${pkgdir}${MINGW_PREFIX}/lib/python"*
}
